package hk.edu.polyu.comp.motioncapture.testing;

import com.jacob.activeX.*;
import com.jacob.com.*;

public class TestOptiTrack {

	final static int g_switchState = 0x0C;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Start");
		ComThread.InitSTA();

		ActiveXComponent objNPCameraCollection = new ActiveXComponent(
				"OptiTrack.NPCameraCollection");

		DispatchEvents camerasEvent = new DispatchEvents(objNPCameraCollection,
				new CamerasEventListener());
		if (camerasEvent == null) {
			System.out.println("ERROR in creating event listener");
			return;
		}

		objNPCameraCollection.invoke("Enum");

		int cameraCount = objNPCameraCollection.getPropertyAsInt("Count");
		System.out.println("Camera count: " + cameraCount);
		System.out.println();

		Variant vi = new Variant();
		for (int i = 0; i < cameraCount; i++) {
			vi.putInt(i);
			Dispatch camera = objNPCameraCollection.invoke("Item", vi)
					.getDispatch();

			Dispatch.call(camera, "Open");

			int serialNumber = Dispatch.get(camera, "SerialNumber").getInt();
			System.out.format("Camera %d - Serial number: %d\n", i,
					serialNumber);

			int model = Dispatch.get(camera, "Model").getInt();
			System.out.format("Camera %d - Model: %s\n", i, Integer
					.toHexString(model).toUpperCase());

			int revision = Dispatch.get(camera, "Revision").getInt();
			System.out.format("Camera %d - Revision: %s\n", i, Integer
					.toHexString(revision).toUpperCase());

			int width = Dispatch.get(camera, "Width").getInt();
			System.out.format("Camera %d - Width: %d\n", i, width);

			int height = Dispatch.get(camera, "Height").getInt();
			System.out.format("Camera %d - Height: %d\n", i, height);

			int frameRate = Dispatch.get(camera, "FrameRate").getInt();
			System.out.format("Camera %d - FrameRate: %d\n", i, frameRate);

			DispatchEvents cameraEvent = new DispatchEvents(camera,
					new CameraEventListener());
			if (cameraEvent == null) {
				System.out.println("ERROR in creating event listener");
				break;
			}

			Dispatch.call(camera, "Start");

			// set the option to get empty frames so we can detect switch
			// changes without any image data
			// 'Camera.SetOption 41, TRUE
			// Dispatch.call(camera, "SetOption", new Variant(41), new
			// Variant(true));

			for (int n = 0; n < 4; n++) {
				System.out.println(n + "...");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			Dispatch.call(camera, "Stop");

			Dispatch.call(camera, "Close");
		}

		ComThread.Release();
		System.out.println("End");
	}

}
